
###Script to Code variables---------------------------

###Clear R------------------------
rm(list=ls())

####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

packages <- c("car", "psych","moments", "lavaan")
ipak(packages)

###Function to recode variables to range from lowest (0) to highest (1) observation
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}
###load Data.Rdta-----------------
load("Study1_replication.RData")

### Code treatment-------------------------------
data$Treatment <- NA
data$Treatment[data$T_Control==1]=1 ## Control
data$Treatment[data$T_DemProp_RepOp==1]=2 ## Dems Support, Reps Oppose
data$Treatment[data$T_RepProp_DemOp==1]=3 ## Reps Support, Dems Oppose

### Partisan Social Identity Strength, Rs were assigned to answer about their party
## new variable that merges both sets of questions
data$huddy1 <- ifelse(!is.na(data$huddyid_dem_1),data$huddyid_dem_1,data$Q11_1)
data$huddy2 <- ifelse(!is.na(data$huddyid_dem_2),data$huddyid_dem_2,data$Q11_2)
data$huddy3 <- ifelse(!is.na(data$huddyid_dem_3),data$huddyid_dem_3,data$Q11_3)
data$huddy4 <- ifelse(!is.na(data$huddyid_dem_4),data$huddyid_dem_4,data$Q11_4)
data$huddy5 <- ifelse(!is.na(data$huddyid_dem_5),data$huddyid_dem_5,data$Q11_5)
data$huddy6 <- ifelse(!is.na(data$huddyid_dem_6),data$huddyid_dem_6,data$Q11_6)
data$huddy7 <- ifelse(!is.na(data$huddyid_dem_7),data$huddyid_dem_7,data$Q11_7)
data$huddy8 <- ifelse(!is.na(data$huddyid_dem_8),data$huddyid_dem_8,data$Q11_8)

#PID, rowMeans and reverse code after transforming to zero and 1
data$dempid <- 1-rowMeans(mapply(zero1,with(data,data.frame(huddyid_dem_1,huddyid_dem_2,huddyid_dem_3,huddyid_dem_4,huddyid_dem_5,huddyid_dem_6,huddyid_dem_7,huddyid_dem_8))),na.rm=T)
data$reppid <- 1-rowMeans(mapply(zero1,with(data,data.frame(Q11_1,Q11_2,Q11_3,Q11_4,Q11_5,Q11_6,Q11_7,Q11_8))),na.rm=T)
data$partyidentity <- rowMeans(with(data,data.frame(dempid,reppid)),na.rm = T)

### Subset data, remove those who weren't in any treatment group, remove pure moderates
data <- subset(data,is.na(Treatment)==F)
data <- subset(data,is.na(partyidentity)==F)


### Dependent variable----------------------------
#Item 1 (Kam: support for irradiation)
#Recode so that opposition is the highest value
data$SupportIrradiation_rec<-(6-data$SupportIrradiation)

#Item 2: Costs outweigh benefits
#recode so that opposition is the highest value
data$CostsFoodIrradiation_rec<-(6-data$CostsFoodIrradiation)

#Item 3: food irradiation is bad
#recode so that opposition is the highest value
data$IrradiationGood_rec<-(6-data$IrradiationGood)

#create scale: high score is ban irradiation
data$DV_irradiation<-rowMeans(data.frame(data$SupportIrradiation_rec+ data$CostsFoodIrradiation_rec+ data$IrradiationGood_rec), na.rm=T)
#recode to range from 0 to 1
data$DV_irradiation<-zero1(data$DV_irradiation)

### Need for Cognition----------------------
#reversed coded itmes recoded
data$NfC_3_rec<-(6-data$NfC_3)
data$NfC_4_rec<-(6-data$NfC_4)
data$NfC_5_rec<-(6-data$NfC_5)
data$NfC_7_rec<-(6-data$NfC_7)
data$NfC_8_rec<-(6-data$NfC_8)
data$NfC_9_rec<-(6-data$NfC_9)
data$NfC_12_rec<-(6-data$NfC_12)
data$NfC_16_rec<-(6-data$NfC_16)
data$NfC_17_rec<-(6-data$NfC_17)

#create 18-item scale
data$nfc<-rowMeans(data.frame(data$NfC_1,data$NfC_2, data$NfC_3_rec, data$NfC_4_rec, data$NfC_5_rec, data$NfC_6, data$NfC_7_rec, data$NfC_8_rec, data$NfC_9_rec, data$NfC_10, data$NfC_11, data$NfC_12_rec, data$NfC_13, data$NfC_14, data$NfC_15, data$NfC_16_rec, data$NfC_17_rec, data$NfC_18), na.rm=T)
data$nfc<-zero1(data$nfc)

### Cognitive Reflection Test-------------------------------------
data$CRT1 <- ifelse(data$cr1 %in% c("5",".05","0,05","0.05"),1,0)
data$CRT2 <- ifelse(data$cr2 %in% c("5","05","5.00"),1,0)
data$CRT3 <-  ifelse(data$cr3 %in% c("47"),1,0)
cor(with(data,data.frame(CRT1,CRT2,CRT3)),use="pairwise.complete.obs")
data$CRTall <- rowSums(with(data,data.frame(CRT1,CRT2,CRT3)),na.rm=T)
data$CRTall <- zero1(data$CRTall) ##recode to range from 0-1

### Latent cognitive resources --------------------------
latent <- 'cogresources  =~ NA*CRT1+CRT2+CRT3+NfC_1 + NfC_2 + NfC_3_rec + NfC_4_rec + NfC_5_rec + NfC_6+ NfC_7_rec+NfC_8_rec+NfC_9_rec+NfC_10+NfC_11+NfC_12_rec+NfC_13+NfC_14+NfC_15+NfC_16_rec+NfC_17_rec+NfC_18
# fix variance of latent variable
cogresources ~~ 1*cogresources'

fit <- cfa(latent, ordered=c("CRT1", "CRT2", "CRT3", "NFC_1", "NFC_2", "NFC_3_rec", "NFC_4_rec", "NFC_5_rec", "NfC_6", "NFC_7_rec", "NfC_8_rec", "NfC_9_rec", "NFC_10", "NFC_11", "NFC_12_rec", "NFC_13", "NfC_14", "NfC_15", "NfC_16_rec", "NfC_17_rec", "NfC_18"), data=data)
summary(fit, fit.measures=TRUE)

#fit model
data$cogresources<-(lavPredict(fit, type = "lv", method="EBM", label = TRUE, optim.method = "nlminb")) 
data$cogresources<-zero1(data$cogresources)
#In-party
# In-party endorsement: 0 (out-party endorses ban or no party cues); 1 (leaning, weak or strong partisan, in-party endorses ban). 
#Inparty endorsement
data$InParty<-NA
data$InParty[(data$Treatment==2 & data$pid1==1) | (data$Treatment==2 & data$pid4==1) | (data$Treatment==3 & data$pid1==2 )| (data$Treatment==3 & data$pid4==2 )|data$Treatment==1]=0
data$InParty[(data$Treatment==2 & data$pid1==2 )| (data$Treatment==3 & data$pid1==1 )]=1
data$InParty[(data$Treatment==2 & data$pid4==2 )| (data$Treatment==3 & data$pid4==1) ]=1

#Out-party
#Out-party endorsement: 0 (in-party endorses ban or no party cues); 1 (leaning partisan; weak or strong partisan, out-party endorses ban).
#outparty
data$OutParty<-NA
data$OutParty[data$Treatment==2 & data$pid1==2 | data$Treatment==2 & data$pid4==2 | data$Treatment==3 & data$pid1==1 | data$Treatment==3 & data$pid4==1 | data$Treatment==1]=0
data$OutParty[data$Treatment==2 & data$pid1==1 | data$Treatment==3 & data$pid1==2 ]=1
data$OutParty[data$Treatment==2 & data$pid4==1 | data$Treatment==3 & data$pid4==2 ]=1

### In-party cue versus Out-party cue-----------------------
data$In_vs_OutParty[(data$Treatment==2 & data$pid1==1) | (data$Treatment==2 & data$pid4==1) | (data$Treatment==3 & data$pid1==2 )| (data$Treatment==3 & data$pid4==2 )|data$Treatment==1]=0
data$In_vs_OutParty[(data$Treatment==2 & data$pid1==2 )| (data$Treatment==3 & data$pid1==1 )]=1
data$In_vs_OutParty[(data$Treatment==2 & data$pid4==2 )| (data$Treatment==3 & data$pid4==1) ]=1
data$In_vs_OutParty[data$Treatment==1]=NA

### Covariates---------------------
#gender: male==0, female==1
data$female[data$Q110==1]<-0
data$female[data$Q110==2]<-1
#Age
data$age<-(2016-as.numeric(data$Q116, na.rm=T))

#Race
data$non_white<-ifelse(data$race==1,0,1)

#Education
data$education<-data$hs
data$education<-car::recode(data$education, "2=1; 3=2; 4=2; 5=3; 6=3; 7=3")


#party
data$Republican_dummy<-0
data$Republican_dummy[data$pid1==1]=1
data$Republican_dummy[data$pid4==1]=1

#Income
data$income<-data$Q114

#Partisanship
data$partisanship <- NA
data$partisanship[which(data$pid4==3)]=4
data$partisanship[which(data$pid4==1)]=3
data$partisanship[which(data$pid4==2)]=5
data$partisanship[which(data$pid3==2)]=2
data$partisanship[which(data$pid3==1)]=1
data$partisanship[which(data$pid2==2)]=6
data$partisanship[which(data$pid2==1)]=7
data$pidstrength<-abs(data$partisanship-4)


#partisanship without independents
data$partisanship_ind <- NA
data$partisanship_ind[which(data$pid3==1)]=1
data$partisanship_ind[which(data$pid3==2)]=2
data$partisanship_ind[which(data$pid4==1)]=3
data$partisanship_ind[which(data$pid4==2)]=4
data$partisanship_ind[which(data$pid2==2)]=5
data$partisanship_ind[which(data$pid2==1)]=6


save(data, file="Study1_data.RData")

